﻿using System.Windows.Interactivity;
using Ops.Mpc.Presenters;
using System.Windows.Media;
using System.Windows;
using Ops.Mpc.Controllers;
using System;

namespace Ops.Mpc.States
{
    public class TriggerStateAction : TriggerAction<UIElement>
    {
        #region Fields & Properties
        public static readonly DependencyProperty StateActionItemNameProperty = DependencyProperty.Register("StateActionItemName", typeof(string), typeof(TriggerStateAction), null);

        public string StateActionItemName
        {
            get
            {
                return (string)this.GetValue(StateActionItemNameProperty);
            }

            set
            {
                this.SetValue(StateActionItemNameProperty, value);
            }

        }
        #endregion

        #region Methods

        protected override void Invoke(object parameter)
        {
            IStateAction stateAction = this.GetStateAction();

            if (stateAction != null)
                stateAction.Execute(parameter, new ActionResultCallback(new ActionResultCallback(OnStateActionComplete)));

        }

        private void OnStateActionComplete(ActionResult actionResult)
        {

        }

        protected Presenter GetPresenter()
        {
            return Presenter.GetParentPresenter(this.AssociatedObject);

        }

        protected IController GetController()
        {
            Presenter presenter = this.GetPresenter(); ;

            if (presenter != null)
                return presenter.Controller;
            else
                return null;
        }

        protected IState GetCurrentState()
        {
            IController iController = this.GetController();

            if (iController != null)
                return iController.CurrentState;
            else
                return null;
        }

        protected IStateAction GetStateAction()
        {
            IState iState = this.GetCurrentState();

            if (iState != null)
                return iState.GetStateAction(this.StateActionItemName, true);
            else
                return null;
        }
        #endregion
    }
}
